﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Web;
using System.Net;
using System.IO;
using System.Xml.Linq;

namespace SharePointUserExport
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ExportUser();
        }


        private void ExportUser()
        {


            try
            {
                string Path = txtSPAddress.Text + "_vti_bin/listdata.svc/UserInformationList";

            string s = HttpPost(Path);

          


            XNamespace dsPeople = "http://schemas.microsoft.com/ado/2007/08/dataservices" ;

            XNamespace aw = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";




            File.AppendAllText(txtExportLocation.Text, GetFormatedText("AccountName") + ","
                      + GetFormatedText("WindowsLoginName") + "," +
                      GetFormatedText("WorkEMail") + "," +
                      GetFormatedText("Deleted") + "," +
                      GetFormatedText("Department") + "," +
                      GetFormatedText("Title") + "," +
                      GetFormatedText("FirstName") + "," +
                      GetFormatedText("LastName") + "," +
                      GetFormatedText("UserName") + "," +
                      GetFormatedText("Office") + "," +
                      GetFormatedText("SubDepartment") + "," +
                      GetFormatedText("Id") + "," +
                      GetFormatedText("ContentType") + System.Environment.NewLine, Encoding.Default);



            XElement po = XElement.Load(new StringReader(s));
            IEnumerable<XElement> items =
            from el in po.Descendants(aw + "properties")

            select el;
            foreach (XElement prdName in items)
            {

                string AccountName = prdName.Descendants(dsPeople + "Name").FirstOrDefault().Value;
                string WindowsLoginName = prdName.Descendants(dsPeople + "Account").FirstOrDefault().Value;
                string WorkEMail = prdName.Descendants(dsPeople + "WorkEMail").FirstOrDefault().Value;
                string Deleted = prdName.Descendants(dsPeople + "Deleted").FirstOrDefault().Value;
                string Department = prdName.Descendants(dsPeople + "Department").FirstOrDefault().Value;
                string Title = prdName.Descendants(dsPeople + "Title").FirstOrDefault().Value;
                string FirstName = prdName.Descendants(dsPeople + "FirstName").FirstOrDefault().Value;
                string LastName = prdName.Descendants(dsPeople + "LastName").FirstOrDefault().Value;
                string UserName = prdName.Descendants(dsPeople + "UserName").FirstOrDefault().Value;
                string Office = prdName.Descendants(dsPeople + "Office").FirstOrDefault().Value;
                string SubDepartment = prdName.Descendants(dsPeople + "SubDepartment").FirstOrDefault().Value;
                string Id = prdName.Descendants(dsPeople + "Id").FirstOrDefault().Value;
                string ContentType = prdName.Descendants(dsPeople + "ContentType").FirstOrDefault().Value;
                
                
                File.AppendAllText(txtExportLocation.Text , GetFormatedText(AccountName) + "," 
                    + GetFormatedText(WindowsLoginName) + "," +
                    GetFormatedText(WorkEMail) + "," +
                    GetFormatedText(Deleted) + "," +
                    GetFormatedText(Department) + "," +
                    GetFormatedText(Title) + "," +
                    GetFormatedText(FirstName) + "," +
                    GetFormatedText(LastName) + "," +
                    GetFormatedText(UserName) + "," +
                    GetFormatedText(Office) + "," +
                    GetFormatedText(SubDepartment) + "," +
                    GetFormatedText(Id) + "," +
                    GetFormatedText(ContentType) +  System.Environment.NewLine ,Encoding.Default);


            }



            txtLog.Text += System.Environment.NewLine;
            txtLog.Text += "user information exported, please check " +  txtExportLocation.Text;
            txtLog.Text += System.Environment.NewLine;

            }
            catch (Exception ex)
            {
                txtLog.Text += ex.Message + ", " +  ex.StackTrace;
            }

        }
        public string GetFormatedText(string s)
        {
            return "\"" + s + "\"";
        }
      
        public string HttpPost(string uri)
        {
           

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);

           
            webRequest.ContentType = "application/xml";
            webRequest.MediaType = "application/xml";
            webRequest.Method = "GET";
            txtLog.Text += uri + System.Environment.NewLine;

          

            webRequest.UseDefaultCredentials = true;
            webRequest.PreAuthenticate = true;
            webRequest.Credentials = CredentialCache.DefaultCredentials;

          

            try
            {

                txtLog.Text += "retrieveing request" + System.Environment.NewLine;


                WebResponse webResponse = webRequest.GetResponse();


                if (webResponse == null)
                { return null; }
                StreamReader sr = new StreamReader(webResponse.GetResponseStream());

                txtLog.Text += "completed request" + System.Environment.NewLine;
                return sr.ReadToEnd().Trim();
            }
            catch (WebException ex)
            {
                txtLog.Text = ex.Message + System.Environment.NewLine;
            }
            catch (Exception ex)
            {
                txtLog.Text = ex.Message + System.Environment.NewLine;
            }
            return null;
        } // end HttpPost 

    }
}
